home *** CD-ROM | disk | FTP | other *** search
/ Total Network Tools 2002 / NextStepPublishing-TotalNetworkTools2002-Win95.iso / Archive / Misc Servers / Zope.exe / PUBLISH.PY < prev    next >
Encoding:
Python Source  |  2000-06-02  |  15.0 KB  |  415 lines

  1. ##############################################################################
  2. # Zope Public License (ZPL) Version 1.0
  3. # -------------------------------------
  4. # Copyright (c) Digital Creations.  All rights reserved.
  5. # This license has been certified as Open Source(tm).
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions are
  8. # met:
  9. # 1. Redistributions in source code must retain the above copyright
  10. #    notice, this list of conditions, and the following disclaimer.
  11. # 2. Redistributions in binary form must reproduce the above copyright
  12. #    notice, this list of conditions, and the following disclaimer in
  13. #    the documentation and/or other materials provided with the
  14. #    distribution.
  15. # 3. Digital Creations requests that attribution be given to Zope
  16. #    in any manner possible. Zope includes a "Powered by Zope"
  17. #    button that is installed by default. While it is not a license
  18. #    violation to remove this button, it is requested that the
  19. #    attribution remain. A significant investment has been put
  20. #    into Zope, and this effort will continue if the Zope community
  21. #    continues to grow. This is one way to assure that growth.
  22. # 4. All advertising materials and documentation mentioning
  23. #    features derived from or use of this software must display
  24. #    the following acknowledgement:
  25. #      "This product includes software developed by Digital Creations
  26. #      for use in the Z Object Publishing Environment
  27. #      (http://www.zope.org/)."
  28. #    In the event that the product being advertised includes an
  29. #    intact Zope distribution (with copyright and license included)
  30. #    then this clause is waived.
  31. # 5. Names associated with Zope or Digital Creations must not be used to
  32. #    endorse or promote products derived from this software without
  33. #    prior written permission from Digital Creations.
  34. # 6. Modified redistributions of any form whatsoever must retain
  35. #    the following acknowledgment:
  36. #      "This product includes software developed by Digital Creations
  37. #      for use in the Z Object Publishing Environment
  38. #      (http://www.zope.org/)."
  39. #    Intact (re-)distributions of any official Zope release do not
  40. #    require an external acknowledgement.
  41. # 7. Modifications are encouraged but must be packaged separately as
  42. #    patches to official Zope releases.  Distributions that do not
  43. #    clearly separate the patches from the original work must be clearly
  44. #    labeled as unofficial distributions.  Modifications which do not
  45. #    carry the name Zope may be packaged in any form, as long as they
  46. #    conform to all of the clauses above.
  47. # Disclaimer
  48. #   THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS ``AS IS'' AND ANY
  49. #   EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  50. #   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  51. #   PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL DIGITAL CREATIONS OR ITS
  52. #   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  53. #   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  54. #   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  55. #   USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  56. #   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  57. #   OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  58. #   OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  59. #   SUCH DAMAGE.
  60. # This software consists of contributions made by Digital Creations and
  61. # many individuals on behalf of Digital Creations.  Specific
  62. # attributions are listed in the accompanying credits file.
  63. ##############################################################################
  64. __doc__="""Python Object Publisher -- Publish Python objects on web servers
  65.  
  66. $Id: Publish.py,v 1.147 2000/06/02 20:03:25 jim Exp $"""
  67. __version__='$Revision: 1.147 $'[11:-2]
  68.  
  69. import sys, os
  70. from string import lower, atoi, rfind, strip
  71. from Response import Response
  72. from Request import Request
  73. from maybe_lock import allocate_lock
  74. from mapply import mapply
  75.  
  76. class Retry(Exception):
  77.     """Raise this to retry a request
  78.     """
  79.  
  80.     def __init__(self, t=None, v=None, tb=None):
  81.         self._args=t, v, tb
  82.  
  83.     def reraise(self):
  84.         t, v, tb = self._args
  85.         if t is None: t=Retry
  86.         if tb is None: raise t, v
  87.         try: raise t, v, tb
  88.         finally: tb=None
  89.  
  90. def call_object(object, args, request):
  91.     result=apply(object,args) # Type s<cr> to step into published object.
  92.     return result
  93.  
  94. def missing_name(name, request):
  95.     if name=='self': return request['PARENTS'][0]
  96.     request.response.badRequestError(name)
  97.  
  98. def dont_publish_class(klass, request):
  99.     request.response.forbiddenError("class %s" % klass.__name__)
  100.  
  101. def publish(request, module_name, after_list, debug=0,
  102.             # Optimize:
  103.             call_object=call_object,
  104.             missing_name=missing_name,
  105.             dont_publish_class=dont_publish_class,
  106.             mapply=mapply,
  107.             ):
  108.  
  109.     (bobo_before, bobo_after, object, realm, debug_mode, err_hook,
  110.      validated_hook, transactions_manager)= get_module_info(module_name)
  111.  
  112.     parents=None
  113.  
  114.     try:
  115.         request.processInputs()
  116.  
  117.         request_get=request.get
  118.         response=request.response
  119.     
  120.         # First check for "cancel" redirect:
  121.         cancel=''
  122.         if lower(strip(request_get('SUBMIT','')))=='cancel':
  123.             cancel=request_get('CANCEL_ACTION','')
  124.             if cancel: raise 'Redirect', cancel
  125.     
  126.         after_list[0]=bobo_after
  127.         if debug_mode: response.debug_mode=debug_mode
  128.         if realm and not request.get('REMOTE_USER',None):
  129.             response.realm=realm
  130.     
  131.         if bobo_before is not None:
  132.             bobo_before()
  133.     
  134.         # Get a nice clean path list:
  135.         path=strip(request_get('PATH_INFO'))
  136.     
  137.         request['PARENTS']=parents=[object]
  138.         
  139.         if transactions_manager: transactions_manager.begin()
  140.     
  141.         object=request.traverse(path, validated_hook=validated_hook)
  142.     
  143.         if transactions_manager:
  144.             transactions_manager.recordMetaData(object, request)
  145.     
  146.         result=mapply(object, request.args, request,
  147.                       call_object,1,
  148.                       missing_name, 
  149.                       dont_publish_class,
  150.                       request, bind=1)
  151.     
  152.         if result is not response: response.setBody(result)
  153.     
  154.         if transactions_manager: transactions_manager.commit()
  155.  
  156.         return response
  157.     except:
  158.         if transactions_manager: transactions_manager.abort()
  159.         
  160.         if err_hook is not None:
  161.             if parents: parents=parents[0]
  162.             try:
  163.                 return err_hook(parents, request,
  164.                                 sys.exc_info()[0],
  165.                                 sys.exc_info()[1],
  166.                                 sys.exc_info()[2],
  167.                                 )
  168.             except Retry:
  169.                 # We need to try again....
  170.                 if not request.supports_retry():
  171.                     return err_hook(parents, request,
  172.                                     sys.exc_info()[0],
  173.                                     sys.exc_info()[1],
  174.                                     sys.exc_info()[2],
  175.                                     )
  176.                 newrequest=request.retry()
  177.                 try:
  178.                     return publish(newrequest, module_name, after_list, debug)
  179.                 finally:
  180.                     newrequest.close()
  181.                     
  182.         else: raise
  183.             
  184.  
  185. def publish_module(module_name,
  186.                    stdin=sys.stdin, stdout=sys.stdout, stderr=sys.stderr,
  187.                    environ=os.environ, debug=0, request=None, response=None):
  188.     must_die=0
  189.     status=200
  190.     after_list=[None]
  191.     try:
  192.         try:
  193.             if response is None:
  194.                 response=Response(stdout=stdout, stderr=stderr)
  195.             else:
  196.                 stdout=response.stdout
  197.  
  198.             if request is None:
  199.                 request=Request(stdin, environ, response)
  200.  
  201.             response = publish(request, module_name, after_list, debug=debug)
  202.         except SystemExit, v:
  203.             must_die=sys.exc_info()
  204.             request.response.exception(must_die)
  205.         except ImportError, v:
  206.             if type(v) is type(()) and len(v)==3: must_die=v
  207.             elif hasattr(sys, 'exc_info'): must_die=sys.exc_info()
  208.             else: must_die = SystemExit, v, sys.exc_info()[2]
  209.             request.response.exception(1, v)
  210.         except:
  211.             request.response.exception()
  212.             status=response.getStatus()
  213.  
  214.         if response:
  215.             outputBody=getattr(response, 'outputBody', None)
  216.             if outputBody is not None:
  217.                 outputBody()
  218.             else:
  219.                 response=str(response)
  220.                 if response: stdout.write(response)
  221.  
  222.         # The module defined a post-access function, call it
  223.         if after_list[0] is not None: after_list[0]()
  224.  
  225.     finally:
  226.         if request is not None: request.close()
  227.  
  228.     if must_die:
  229.         try: raise must_die[0], must_die[1], must_die[2]
  230.         finally: must_die=None
  231.  
  232.     return status
  233.  
  234.  
  235. _l=allocate_lock()
  236. def get_module_info(module_name, modules={},
  237.                     acquire=_l.acquire,
  238.                     release=_l.release,
  239.                     ):
  240.  
  241.     if modules.has_key(module_name): return modules[module_name]
  242.  
  243.     if module_name[-4:]=='.cgi': module_name=module_name[:-4]
  244.  
  245.     acquire()
  246.     tb=None
  247.     try:
  248.         try:
  249.             module=__import__(module_name, globals(), globals(), ('__doc__',))
  250.  
  251.             realm=module_name
  252.  
  253.             # Let the app specify a realm
  254.             if hasattr(module,'__bobo_realm__'):
  255.                 realm=module.__bobo_realm__
  256.             elif os.environ.has_key('Z_REALM'):
  257.                 realm=os.environ['Z_REALM']
  258.             elif os.environ.has_key('BOBO_REALM'):
  259.                 realm=os.environ['BOBO_REALM']
  260.             else: realm=module_name
  261.  
  262.             # Check for debug mode
  263.             if hasattr(module,'__bobo_debug_mode__'):
  264.                 debug_mode=not not module.__bobo_debug_mode__
  265.             elif (os.environ.has_key('Z_DEBUG_MODE') or
  266.                   os.environ.has_key('BOBO_DEBUG_MODE')):
  267.                 if os.environ.has_key('Z_DEBUG_MODE'):
  268.                     debug_mode=lower(os.environ['Z_DEBUG_MODE'])
  269.                 else:
  270.                     debug_mode=lower(os.environ['BOBO_DEBUG_MODE'])
  271.                 if debug_mode=='y' or debug_mode=='yes':
  272.                     debug_mode=1
  273.                 else:
  274.                     try: debug_mode=atoi(debug_mode)
  275.                     except: debug_mode=None
  276.             else: debug_mode=None
  277.  
  278.             if hasattr(module,'__bobo_before__'):
  279.                 bobo_before=module.__bobo_before__
  280.             else: bobo_before=None
  281.  
  282.             if hasattr(module,'__bobo_after__'): bobo_after=module.__bobo_after__
  283.             else: bobo_after=None
  284.  
  285.             if hasattr(module,'bobo_application'):
  286.                 object=module.bobo_application
  287.             elif hasattr(module,'web_objects'):
  288.                 object=module.web_objects
  289.             else: object=module
  290.  
  291.             error_hook=getattr(module,'zpublisher_exception_hook', None)
  292.             validated_hook=getattr(module,'zpublisher_validated_hook', None)
  293.  
  294.             transactions_manager=getattr(
  295.                 module,'zpublisher_transactions_manager', None)
  296.             if not transactions_manager:
  297.                 try: get_transaction()
  298.                 except: pass
  299.                 else:
  300.                     # Create a default transactions manager for use
  301.                     # by software that uses ZPublisher and ZODB but
  302.                     # not the rest of Zope.
  303.                     transactions_manager = DefaultTransactionsManager()
  304.  
  305.             info= (bobo_before, bobo_after, object, realm, debug_mode,
  306.                    error_hook, validated_hook, transactions_manager)
  307.  
  308.             modules[module_name]=modules[module_name+'.cgi']=info
  309.             
  310.             return info
  311.         except:
  312.             t,v,tb=sys.exc_info()
  313.             v=str(v)
  314.             raise ImportError, (t, v), tb
  315.     finally:
  316.         tb=None
  317.         release()
  318.  
  319.  
  320. class DefaultTransactionsManager:
  321.     def begin(self): get_transaction().begin()
  322.     def commit(self): get_transaction().commit()
  323.     def abort(self): get_transaction().abort()
  324.     def recordMetaData(self, object, request):
  325.         # Is this code needed?
  326.         request_get = request.get
  327.         T=get_transaction()
  328.         T.note(request_get('PATH_INFO'))
  329.         auth_user=request_get('AUTHENTICATED_USER',None)
  330.         if auth_user is not None:
  331.             T.setUser(auth_user, request_get('AUTHENTICATION_PATH'))
  332.         
  333.  
  334. # ZPublisher profiler support
  335. # ---------------------------
  336.  
  337. if os.environ.get('PROFILE_PUBLISHER', None):
  338.  
  339.     import profile, pstats
  340.  
  341.     _pfile=os.environ['PROFILE_PUBLISHER']
  342.     _plock=allocate_lock()
  343.     _pfunc=publish_module
  344.     _pstat=None
  345.  
  346.     def pm(module_name, stdin, stdout, stderr, 
  347.            environ, debug, request, response):
  348.         try:
  349.             r=_pfunc(module_name, stdin=stdin, stdout=stdout, 
  350.                      stderr=stderr, environ=environ, debug=debug, 
  351.                      request=request, response=response)
  352.         except: r=None
  353.         sys._pr_=r
  354.  
  355.     def publish_module(module_name, stdin=sys.stdin, stdout=sys.stdout, 
  356.                        stderr=sys.stderr, environ=os.environ, debug=0, 
  357.                        request=None, response=None):
  358.         _plock.acquire()
  359.         try:
  360.             if request is not None:
  361.                 path_info=request.get('PATH_INFO')
  362.             else: path_info=environ.get('PATH_INFO')
  363.             if path_info[-14:]=='manage_profile':
  364.                 return _pfunc(module_name, stdin=stdin, stdout=stdout, 
  365.                               stderr=stderr, environ=environ, debug=debug, 
  366.                               request=request, response=response)
  367.             pobj=profile.Profile()
  368.             pobj.runcall(pm, module_name, stdin, stdout, stderr, 
  369.                          environ, debug, request, response)
  370.             result=sys._pr_
  371.             pobj.create_stats()
  372.             if _pstat is None:
  373.                 global _pstat
  374.                 _pstat=sys._ps_=pstats.Stats(pobj)
  375.             else: _pstat.add(pobj)
  376.         finally:
  377.             _plock.release()
  378.  
  379.         if result is None:
  380.             try:
  381.                 error=sys.exc_info()
  382.                 file=open(_pfile, 'w')
  383.                 sys.stdout=file
  384.                 _pstat.strip_dirs().sort_stats('cumulative').print_stats(250)
  385.                 _pstat.strip_dirs().sort_stats('time').print_stats(250)
  386.                 file.flush()
  387.                 file.close()
  388.             except: pass
  389.             raise error[0], error[1], error[2]
  390.         return result
  391.  
  392.  
  393.  
  394.